草庐IT

lua - redis-lua 在需要时返回 nil

全部标签

ruby-on-rails - 不将 nil 值分配给哈希

当事物在ruby​​中为nil时,是否有将事物分配给哈希的简写或最佳实践?例如,我的问题是我正在使用另一个散列来构建它,如果其中的某些内容为nil,它会将nil分配给该键,而不是让它单独存在。我明白为什么会这样,所以我的解决方案是:hash1[:key]=hash2[:key]unlesshash2[:key].nil?因为我不能在键实际指向nil的has中有一个值。(我宁愿有一个空散列,也不愿有一个有{:key=>nil}的散列,这是不可能发生的)我的问题是有没有更好的方法来做到这一点?我不想在作业结束时执行delete_if。 最佳答案

ruby - 你如何让 DateTime.parse 返回你所在时区的时间?

我需要这个require'date'DateTime.parse"Mon,Dec276:30pm"返回EDT时区下午6:30的DateTime,但返回UTC时间。如何获取EST日期时间或将UTC日期时间转换为具有下午6:30值的EDT日期时间? 最佳答案 好的,我要回答我自己的问题require'time'ENV["TZ"]="US/Eastern"Time.parse("Mon,Dec276:30pm").to_datetime=># 关于ruby-你如何让DateTime.parse

ruby - 需要文件而不执行代码?

这里我有两个文件:文件.rbdefmethodputs"Thiswon'tbeoutputted."endputs"Thiswillbeoutputted."主.rbrequire"./file"当运行main.rb时,它会加载file.rb中的所有代码,所以我会得到“这将被输出”。在屏幕上。是否可以在不运行代码的情况下加载文件?因为我想加载所有方法(也在模块和类中)而不必在这些范围之外执行代码。 最佳答案 Isitpossibletoloadafilewithouthavingittorunthecode?不,ruby文件中的所有

ruby-on-rails - 检查 ActiveRecord 查找是否返回结果

我正在尝试检查查找方法是否返回结果。我的查找方法如下:post=Post.find(:all,:conditions=>{:url=>params['url']},:limit=>1)检查post是否包含结果的好方法是什么? 最佳答案 find:all如果没有返回任何行,则返回一个空数组([]),因此您可以这样使用它:post=Post.find(:all,:conditions=>{:url=>params['url']},:limit=>1)unlesspost.empty?#dosomething...end顺便说一句,如果您

ruby-on-rails - 尝试更新数据库值时,rails update_attributes 返回 false

希望这里有人能给我指出正确的方向。我有一个ControllerUpdatedef运行“update_attributes”。目前它返回false,没有错误消息。我是Ruby的新手,但不是编码的新手,这让我困惑了好几天!我正在尝试使用下面指定的值更新用户模型和数据库。defupdate#getcurrentlyloggedinuser@user=current_user#updateuserparamsbasedoneditform...if@user.update_attributes(params[:user])redirect_toprofile_path,:notice=>"Su

ruby - 未定义方法 '>' 为 nil :NilClass <NoMethodError>

好的,我有下面的代码defupdate_state_actionsstates.eachdo|state|@state_turns[state.id]-=1if@state_turns[state.id]>0&&state.auto_removal_timing==1endend现在在......@state_turns[state.id]-=1if@state_turns[state.id]>0&&state.auto_removal_timing==1它说错误in'blockupdate_state_actions':Undefinedmethod'>'fornil:NilClas

ruby - 全新安装 OSX 10.9.1 在尝试安装 gems 时返回 "undefined method ` path2class'"

我刚刚使用Homebrew和RVM安装了一个干净的Mavericks安装。brewdoctor和rvmrequirements都返回“allgood”,但是,当我在我的项目目录中运行bundleinstall时,我的大多数gem安装都很好,但少数安装失败并出现相同的以下错误:Bundler::GemspecError:Couldnotreadgemat/Users/NK/.rvm/gems/ruby-2.0.0-p353/cache/eventmachine-1.0.3.gem.Itmaybecorrupted.Anerroroccurredwhileinstallingeventma

ruby-on-rails - 我如何解决 "Missing host to link to! Please provide the :host parameter"? (返回率)

我正在关注RoRTutorial我被困在Listing9.15运行'bundleexecrspecspec/'后出现以下错误:1)AuthenticationauthorizationaswrongusersubmittingaPATCHrequesttotheUsers#updateactionFailure/Error:specify{expect(response).toredirect_to(root_url)}ArgumentError:Missinghosttolinkto!Pleaseprovidethe:hostparameter,setdefault_url_opti

Ruby 未定义方法 `[]' 为 nil :NilClass (NoMethodError)

我正在尝试使用ruby​​制作一个Conway的人生游戏版本。我用@play_area创建了一个Grid类作为实例变量。但是,当我运行我的代码时,@play_area在已经被评估两次之后变为nil(当在行中评估时if@play_area[x_mod][y_mod].alive)。为什么会这样?编辑这是初始化函数:definitialize(sizex,sizey)@x_length=sizex@y_length=sizey@play_area=[]#initializedeadcells@x_length.timesdo|x|@play_area[x]||=[]@y_length.ti

ruby - 为什么需要 ":nodoc:"语法?

似乎很多库/插件都使用这种语法:defself.included(base)#:nodoc:base.extendClassMethodsend为什么:nodoc:部分是必需的? 最佳答案 没有必要。如果应用于一个类,它只会抑制类扩展中所有方法的文档(rdoc)。在ProgrammingRuby中描述为::nodoc:-Don'tincludethiselementinthedocumentation.Forclassesandmodules,themethods,aliases,constants,andattributesdir